package com.gzhryc.shared_device.common.payment.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.base.enums.EPayState;
import com.gzhryc.shared_device.common.payment.dao.WalletPayOrderDao;
import com.gzhryc.shared_device.common.payment.dao.db.WalletPayOrder;
import com.gzhryc.shared_device.common.payment.services.dto.WalletPayOrderSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class WalletPayOrderService {

    static Logger log = Logger.getLogger(WalletPayOrderService.class);

    public static WalletPayOrderService self(Date date) {
        return MultiDBTools.getService(WalletPayOrderService.class, date);
    }

    public static WalletPayOrderService self(String jdbcKey,Date date) {
        return MultiDBTools.getService(jdbcKey, WalletPayOrderService.class, date);
    }

    WalletPayOrderDao dao;

    public WalletPayOrderService(String jdbcKey,Date date) {
        dao = new WalletPayOrderDao(jdbcKey,date);
    }

    public List<WalletPayOrder> findBySearch(WalletPayOrderSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        conditions.desc("createDate");

        try {
            return dao.findByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public List<WalletPayOrder> findByBusinessTradeNo(String businessTradeNo){
        if(StringTools.isNotBlank(businessTradeNo)){
            Conditions conditions = new Conditions(WalletPayOrder.class);
            conditions.and().is("businessTradeNo",businessTradeNo);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public Long countBySearch(WalletPayOrderSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public WalletPayOrder getByTradeNo(String tradeNo){
        try {
            return dao.getById(tradeNo);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }
    
    public WalletPayOrder getLastByBusinessTradeNo(String businessTradeNo,Integer payType) {
    	if(StringTools.isNotBlank(businessTradeNo) && payType != null) {
    		Conditions condition = new Conditions(WalletPayOrder.class);
    		condition.and().is("businessTradeNo", businessTradeNo);
    		condition.and().is("payType", payType);
	        try {
	            return dao.get(condition);
	        } catch (SQLException e) {
	            log.error(e.getMessage(),e);
	        }
    	}
        return null;
    }
    
    public List<WalletPayOrder> getSuccessByBusinessTradeNo(String businessTradeNo){
    	if(StringTools.isNotBlank(businessTradeNo)) {
    		Conditions condition = new Conditions(WalletPayOrder.class);
    		condition.and().is("businessTradeNo", businessTradeNo);
    		condition.and().is("payState", EPayState.Success.index());
    		
	        try {
	            return dao.findByCondition(condition);
	        } catch (SQLException e) {
	            log.error(e.getMessage(),e);
	        }
    	}
    	return null;
    }
    
    public boolean closeWaitPay(WalletPayOrder payOrder) {
    	return false;
    }
}
